package dao

import (
	"album/model"
	"context"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
	"listenboos-common/tools"
	"time"
)

type BaseAttributeValueDao struct {
	conn *gorms.GormConn
}

func (u *BaseAttributeValueDao) Save(ctx context.Context, conn db.DBConn, data []*model.BaseAttributeValue) error {
	u.conn = conn.(*gorms.GormConn)
	tx := u.conn.Tx(ctx)
	now := tools.ToTimeString(time.Now().UnixMilli())
	for _, d := range data {
		d.CreateTime = now
		d.UpdateTime = now
	}
	err := tx.Save(&data).Error
	return err
}

func NewBaseAttributeValueDao(db *db.ListenDB) *BaseAttributeValueDao {
	return &BaseAttributeValueDao{
		conn: gorms.New(db.Conn),
	}
}
func (u *BaseAttributeValueDao) FindByAidToValue(ctx context.Context, aid int64) (value []*model.BaseAttributeValue, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseAttributeValue{}).Where("attribute_id = ?", aid).Find(&value).Error
	if err != nil {
		return nil, err
	}
	return value, nil
}
func (u *BaseAttributeValueDao) FindAttributeValuesById(ctx context.Context, vid int64) (value *model.BaseAttributeValue, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.BaseAttributeValue{}).Where("id = ?", vid).Find(&value).Error
	if err != nil {
		return nil, err
	}
	return value, nil
}
